<template>
  <view class="image-list">
    <view class="header">
      <uni-icons 
        type="arrowleft" 
        size="24" 
        color="#333" 
        @click="goBack"
        class="back-icon"
      />
      <text class="header-title">{{ year }}年{{ formatMonth(month) }}图文列表</text>
    </view>

    <view v-if="loading" class="loading">
      <uni-load-more status="loading"></uni-load-more>
    </view>

    <view v-else-if="!loading && galleryList.length === 0" class="empty">
      <text>暂无数据</text>
    </view>

    <scroll-view 
      v-else
      scroll-y="true" 
      class="scroll-view"
      refresher-enabled
      :refresher-triggered="refreshing"
      @refresherrefresh="onRefresh"
    >
      <view class="gallery-list">
        <view 
          class="gallery-item" 
          v-for="(item, index) in galleryList" 
          :key="index"
          @click="previewImage(item)"
        >
          <image 
            :src="item.hp_img_url" 
            mode="widthFix" 
            class="gallery-image"
            lazy-load
          />
          <view class="gallery-info">
            <text class="gallery-title">{{ item.hp_title }}</text>
            <text class="gallery-author">作者: {{ item.author_list[0]?.user_name || '未知' }}</text>
            <text class="gallery-date">{{ formatDate(item.hp_makettime) }}</text>
            <text class="gallery-content">{{ item.hp_content }}</text>
          </view>
        </view>
      </view>
      
      <view class="load-more">
        <uni-load-more :status="loadMoreStatus"></uni-load-more>
      </view>
    </scroll-view>
  </view>
</template>

<script>
import { ref } from 'vue';
import { useRouter } from 'vue-router';

export default {
  onLoad(options) {
    this.initPage(options);
  },

  setup() {
    const year = ref('');
    const month = ref('');
    const galleryList = ref([]);
    const loading = ref(false);
    const refreshing = ref(false);
    const loadMoreStatus = ref('more');
    const router = useRouter();
	
    const initPage = (options) => {
      year.value = options.year;
      month.value = options.month;
      if (year.value && month.value) {
        fetchGallery();
      } else {
        uni.showToast({ title: '参数错误', icon: 'none' });
        goBack();
      }
    };

    const formatMonth = (monthStr) => {
      const months = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'];
      const monthNames = ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'];
      const index = months.indexOf(monthStr);
      return index !== -1 ? monthNames[index] : monthStr;
    };

    const formatDate = (dateString) => {
      if (!dateString) return '';
      const date = new Date(dateString);
      return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')}`;
    };

    const fetchGallery = async () => {
      loading.value = true;
      try {
        const res = await uni.request({
          url: `http://v3.wufazhuce.com:8000/api/hp/bymonth/${year.value}${month.value}`,
          method: 'GET',
          timeout: 10000
        });

        if (res.data && res.data.data) {
          galleryList.value = res.data.data;
        } else {
          uni.showToast({ title: '暂无数据', icon: 'none' });
        }
      } catch (error) {
        console.error('请求失败详情:', error);
        uni.showToast({ title: '网络请求失败', icon: 'none' });
      } finally {
        loading.value = false;
        refreshing.value = false;
      }
    };
    const onRefresh = () => {
      refreshing.value = true;
      fetchGallery();
    };

    const previewImage = (item) => {
      uni.previewImage({
        urls: [item.hp_img_url],
        current: 0
      });
    };

    const goBack = () => {
      router.back();
    };

    return {
      year, month, galleryList, loading, refreshing, loadMoreStatus,
      formatMonth, formatDate, onRefresh, previewImage, goBack,
      initPage 
    };
  }
};
</script>

<style scoped>
.image-list {
  background-color: #f5f5f5;
  min-height: 100vh;
}

.header {
  display: flex;
  align-items: center;
  padding: 15px;
  background-color: #fff;
  border-bottom: 1px solid #eee;
  position: sticky;
  top: 0;
  z-index: 10;
}

.back-icon {
  margin-right: 10px;
}

.header-title {
  font-size: 16px;
  font-weight: bold;
  color: #333;
}

.loading, .empty {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 200px;
}

.empty {
  color: #999;
}

.scroll-view {
  height: calc(100vh - 60px);
}

.gallery-list {
  padding: 10px;
}

.gallery-item {
  background-color: #fff;
  border-radius: 8px;
  margin-bottom: 15px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.gallery-image {
  width: 100%;
  border-radius: 8px 8px 0 0;
}

.gallery-info {
  padding: 15px;
}

.gallery-title {
  font-size: 16px;
  font-weight: bold;
  color: #333;
  display: block;
  margin-bottom: 8px;
}

.gallery-author {
  font-size: 14px;
  color: #666;
  display: block;
  margin-bottom: 5px;
}

.gallery-date {
  font-size: 12px;
  color: #999;
  display: block;
  margin-bottom: 10px;
}

.gallery-content {
  font-size: 14px;
  color: #555;
  line-height: 1.6;
  display: block;
}

.load-more {
  padding: 20px 0;
}
</style>